<?php

namespace Trois\Process;

use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\Component\Process\Config;
use EasySwoole\EasySwoole\Logger;
use Swoole\Process;

class BaseProcess extends AbstractProcess
{
    protected string $processName = ""; // 进程名称
    protected string $processGroup = ""; // 进程组名称
    protected array $arg = []; // 初始化时 传递参数到自定义进程中
    protected bool $enableCoroutine = true; // 自定义进程自动开启协程环境

    public function __construct()
    {
        parent::__construct(new Config([
            'processName' => $this->processName,
            'processGroup' => $this->processGroup,
            'arg' => $this->arg,
            'enableCoroutine' => $this->enableCoroutine,
        ]));
    }

    protected function run($arg)
    {
        $processName = $this->getProcessName(); // 获取 注册进程名称
        $processPid = $this->getPid();  // 获取 当前进程 Pid
        $args = $this->getArg(); // 获取 注册进程时传递的参数
//        $swooleProcess = $this->getProcess();  // 获取 注册进程的实例 \Swoole\Process

        Logger::getInstance()->info($processName . ':' . $processPid . ' 开始自定义进程');
        Logger::getInstance()->info($processName . ':' . $processPid . ' 进程初始化参数：' . json_encode($args, JSON_UNESCAPED_UNICODE));

        $this->start();

    }

    protected function start()
    {

    }

    protected function onPipeReadable(Process $process)
    {

    }
}